package com.campuscarbooking.api.controller;

import com.campuscarbooking.api.dto.ApiResponse;
import com.campuscarbooking.api.dto.auth.LoginResponse;
import com.campuscarbooking.api.dto.auth.PhoneLoginRequest;
import com.campuscarbooking.api.dto.auth.WechatLoginRequest;
import com.campuscarbooking.api.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@Api(tags = "认证接口")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @ApiOperation(value = "微信登录", notes = "使用微信code登录")
    @PostMapping("/wechat")
    public ApiResponse<LoginResponse> wechatLogin(@Valid @RequestBody WechatLoginRequest request) {
        LoginResponse response = authService.wechatLogin(request);
        return ApiResponse.success(response);
    }

    @ApiOperation(value = "手机号登录", notes = "支持两种登录方式：1. 手机号+验证码(loginType=code) 2. 手机号+密码(loginType=password)")
    @PostMapping("/phone")
    public ApiResponse<LoginResponse> phoneLogin(@Valid @RequestBody PhoneLoginRequest request) {
        LoginResponse response = authService.phoneLogin(request);
        return ApiResponse.success(response);
    }

    @ApiOperation(value = "发送验证码", notes = "发送手机验证码")
    @GetMapping("/send-verification-code")
    public ApiResponse<Void> sendVerificationCode(
            @ApiParam(value = "手机号", required = true) 
            @RequestParam 
            @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") 
            String phoneNumber) {
        authService.sendVerificationCode(phoneNumber);
        return ApiResponse.success(null);
    }
} 